package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.OrdDriDto;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.Result;
import com.junwell.entity.Driver;
import com.junwell.entity.Orders;
import com.junwell.entity.User;
import com.junwell.mapper.UserMapper;
import com.junwell.service.DriverService;
import com.junwell.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("findPage")
    public IPage<Orders> findPage( PageDto pageDto) {
        IPage<Orders> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name", pageDto.getQueryString());
        page = ordersService.page(page, queryWrapper);
        return page;
    }

    @PostMapping("add")
    public Result add(@RequestBody Orders orders) {
        boolean add = ordersService.add(orders);
        if (add) {
            return Result.succ("成功");
        } else {
            return Result.fail("添加失败");
        }
    }


    @GetMapping("{id}")
    public Result viewOne(@PathVariable("id") Integer id) {
        Orders orders = ordersService.viewOne(id);
        if (orders != null) {
            return Result.succ(200, "成功", orders);
        } else {
            return Result.fail("失败");
        }
    }


    @PostMapping("update")
    public Result update(@RequestBody Orders orders) {
        boolean update = ordersService.update(orders);
        if (update) {
            return Result.succ("成功");
        } else {
            return Result.fail("失败");
        }
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean delete = ordersService.delete(id);
        if (delete) {
            return Result.succ("成功");
        } else {
            return Result.fail("失败");
        }
    }


    //订单获取所有用户相关数据
    @GetMapping("finduser")
    public Result findUser(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","name","phonenumber","address");
        List<User> users = userMapper.selectList(queryWrapper);
        return  Result.succ(users);
    }




    @PostMapping("shipment")
    public Result updateList(@RequestBody List<Orders> ordersList) {
        boolean b = ordersService.updateList(ordersList);
        if (b) {
            return Result.succ("成功");
        } else {
            return Result.fail("失败");
        }
    }

    @GetMapping("driver")
    public IPage<Driver> page(){
        IPage<Driver> page = new Page<>(1, 10);
        QueryWrapper<Driver> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "");
        page = driverService.page(page, queryWrapper);
        return page;
    }



    @PostMapping("sent")
    public Result updateDriver(@RequestBody OrdDriDto ordDriDto) {
        boolean flag=false;
        List<Orders> ordersList = ordDriDto.getDataList();
        List<Driver> driverList = ordDriDto.getDriverList();
        for (int i=0;i<ordersList.size();i++) {
            for (int j=0;j<driverList.size();j++) {
                if (ordersList.get(i).getUserAddress().equals(driverList.get(j).getWorkarea())){
                     flag= ordersService.sent(driverList.get(j).getId());
                }
            }
        }
        if (flag){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }

    }

    @PostMapping("addPrice")
    public Result addPrice(@RequestBody Orders orders) {
        if (orders.getWeight()<=0.5){
            orders.setPrice(3.0);
        }else {
            orders.setPrice(3.0+ (orders.getWeight()-0.5)*6.0);
        }
        return Result.succ(orders);

    }



}


